home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS15.ADF / C / Pr / print.c < prev    next >
C/C++ Source or Header  |  1988-04-20  |  3KB  |  176 lines

  1. /* print.c
  2.  
  3.  print (C) 1986 by Robert H. Leivian
  4.  
  5.  
  6.     simple print file utility by: Bob Leivian
  7.  
  8.  
  9.     print -nh^lwp:<string> file1 file2 file3 ...
  10.  
  11.     this is designed to be called from the pr driver with the
  12.     expanded list of files to print with a option list
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. char * fgets();
  18.  
  19. int numbers = 0;
  20. int headers = 0;
  21. int control = 0;
  22. int prefix = 0;
  23.  
  24. char theprefix [132];
  25. char buf[136];
  26.  
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     int i;
  32.     char *p;
  33.     FILE *out;
  34.     char buf[136];
  35.     char name[30];
  36.     char c;
  37.  
  38.     if (argc < 2) {
  39.         printf("usage: print -hnpwl^:<string> file1 file2...\n");
  40.         exit(27);
  41.     }
  42.  
  43.     out = fopen("prt:", "w");
  44.     if (!out) {
  45.         printf("can't open the printer\n");
  46.         exit(27);
  47.     }
  48.  
  49.     while (argv[1]) {
  50.         /* process options if any */
  51.         while (*argv[1] == '-') {
  52.             p = (char *) &*argv[1];
  53.  
  54.             p++;        /* point to the option chars */
  55.             do {
  56.                 switch (*p) {
  57.                 case ' ':
  58.                 case '\0':        /* ignore nulls and spaces */
  59.                     break;
  60.  
  61.                 case 'p':        /* turn on proportional mode */
  62.                     fputs("\n\x1b[2p\n", out);
  63.                     break;
  64.  
  65.                 case 'w':        /* turn on wide (132 col) mode */
  66. #define WORKS
  67. #ifdef WORKS
  68.                     fputs("\n\x1b[4w\n", out);
  69. #else
  70.                     fputs("\017", out);
  71. #endif
  72.                     fflush(out);
  73.                     break;
  74.  
  75.                 case 'l':        /* turn on letter quality mode */
  76.                     fputs("\n\x1b[2\"z\n", out);
  77.                     break;
  78.  
  79.                 case ':':        /* print control string first */
  80.                     strcpy(theprefix, ++p);
  81.                     prefix++;
  82.                     goto next; /* consume rest of option */
  83.  
  84.                 case '^':        /* print control chars as ^X */
  85.                     control++;
  86.                 break;
  87.  
  88.                 case 'n':        /* print line numbers */
  89.                     numbers++;
  90.                     break;
  91.  
  92.                 case 'h':        /* print a header */
  93.                     headers++;
  94.                     break;
  95.  
  96.                 default:
  97.                     printf("'%c' is an invalid option\n", *p);
  98.                     exit(27);
  99.                 }
  100.                 p++; 
  101.             } while (*p);
  102.     next:
  103.             argc--;
  104.             argv++;
  105.         }
  106.  
  107.         /* put out the control prefix if any */
  108.         if (prefix) {
  109.             p = theprefix;
  110.             while(*p) {
  111.                 if (*p == '^') { 
  112.                     p++;
  113.                     c = *p - '@';
  114.                     putc(c, out);
  115.                 } else
  116.                     putc(*p, out);
  117.                 p++;
  118.             }
  119.  
  120.             /* reset the prefix flag */
  121.             prefix = 0;
  122.         }
  123.  
  124.         /* now print the file */
  125.         doPrint(argv[1], out);
  126.         argv++;
  127.     }
  128. }
  129.  
  130.  
  131. /* do the actual print for a file */
  132. doPrint(name, out)
  133. char * name;
  134. FILE * out;
  135. {
  136.     extern errno;
  137.     FILE *in;
  138.     int line = 0;
  139.     char *p;
  140.     char buf[136];
  141.  
  142.     in = fopen(name, "r");
  143.  
  144.     if (in) {
  145.         line = 0;
  146.  
  147.         /* print a file header if desired */
  148.         if (headers)
  149.             fprintf(out, "File: %s\n\n", name);
  150.  
  151.         while ((p = fgets(buf, 134, in)) != NULL){
  152.             line++;
  153.  
  154.             /* print a line number id desired */
  155.             if (numbers) 
  156.                 fprintf(out, "%4d ", line);
  157.  
  158.             /* interp control chars as ^X format */
  159.             if (control) {
  160.                 while(*p) {
  161.                     if ((*p < ' ') && (*p != '\n') && (*p != '\t')){
  162.                         putc('^', out);
  163.                         putc(*p + '@', out);
  164.                     } else
  165.                         putc(*p, out);
  166.                     p++;
  167.                 }
  168.             } else
  169.                 fputs(p, out);
  170.          }
  171.         putc('\f', out);
  172.         fclose(in);
  173.     } else
  174.         fprintf(out, "unable to open: '%s', errno: %d\f", name, errno);
  175. }
  176.